<template>
  <el-dialog
    width="1100px"
    :title="$t('查看取货单产品')"
    destroy-on-close
    append-to-body
    :visible.sync="show"
    :before-close="cancel">
    <el-row style="margin-bottom: 10px">
      <el-form :model="queryParams" ref="queryForm" size="small" inline v-show="showSearch">
        <el-form-item prop="skuNo">
          <el-input
            :placeholder="$t('sku')"
            v-model="queryParams.skuNo"
            clearable
            @keyup.enter.native="getList" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">
            {{ $t('搜索') }}
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
            {{ $t('重置') }}
          </el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <el-table
      ref="TableRef"
      :height="calTableHeight() - 150"
      :data="tableData"
      @selection-change="(rows) => (tableCheck = rows)">
      <el-table-column show-overflow-tooltip align="center" :label="$t('sku')" prop="skuNo" width="150" />
      <el-table-column :label="$t('主图')" prop="imgUrl" align="center">
        <template slot-scope="{ row }">
          <el-image
            style="width: 40px; height: 40px"
            :preview-src-list="[row.imgUrl]"
            :src="row.imgUrl + '?x-oss-process=image/resize,w_100/quality,q_70'" />
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        align="center"
        :label="$t('中文名')"
        prop="skuNameCn"
        width="150" />
      <el-table-column
        show-overflow-tooltip
        align="center"
        :label="$t('日文名')"
        prop="skuNameJp"
        width="150" />
      <el-table-column show-overflow-tooltip align="center" :label="$t('可用库存')" prop="stock" />
      <el-table-column show-overflow-tooltip align="center" :label="$t('已选择库存')" prop="" width="150">
        <template slot-scope="{ row }">
          <el-input-number
            v-model="row.num"
            style="width: 100%"
            controls-position="right"
            :precision="0"
            :min="1"
            :max="row.stock" />
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        show-overflow-tooltip
        align="center"
        :label="$t('操作')"
        prop=""
        width="150">
        <template slot-scope="{ row }">
          <el-button
            type="text"
            :disabled="handleDisabled()"
            @click="handleUpdate(row)"
            v-hasPermi="['distribution:skuSellerOfflineSku:edit']">
            修改数量
          </el-button>
          <el-button
            :disabled="handleDisabled()"
            type="text"
            @click="handleDelete(row)"
            v-hasPermi="['distribution:skuSellerOfflineSku:remove']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <div slot="footer">
      <el-button @click="cancel">
        {{ $t('关闭') }}
      </el-button>
    </div>
  </el-dialog>
</template>
<script>
import {
  skuSellerOfflineSkuList,
  editNum,
  skuSellerOfflineSkuDelete,
} from '@/api/orderManage/skuSellerOffline'

export default {
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: () => ({}),
    },
    /**
     * type
     */
    type: {
      type: String,
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      /**显示搜索条件 */
      showSearch: true,
      /**表格选中项 */
      tableCheck: [],
      /**表格数据 */
      tableData: [],
      /**总条数 */
      total: 0,
      /**分页相关查询参数 */
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        this.getList()
      }
    },
  },
  methods: {
    handleDisabled() {
      // <!-- 状态: 1:创建中 2:已提交 3:已完成 4:已取消 -->
      const status = this.formData.status
      let flag = true
      switch (+status) {
        case 1:
          flag = false
          break
        case 2:
          flag = true
          break
        case 3:
          flag = true
          break
        case 4:
          flag = true
          break
        default:
          break
      }

      return flag
    },
    handleDelete(row) {
      this.$confirm(this.$t('确认删除?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        skuSellerOfflineSkuDelete(row.id).then((res) => {
          const { code, msg } = res
          if (code === 200) {
            this.$message({ type: 'success', message: msg })
            this.getList()
          }
        })
      })
    },
    handleUpdate(row) {
      this.$confirm(this.$t('确认修改?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        const data = {
          id: row.id,
          num: row.num,
        }
        editNum(data).then((res) => {
          const { code, msg } = res
          if (code === 200) {
            this.$message({ type: 'success', message: msg })
            this.getList()
          }
        })
      })
    },
    handleSubmit() {
      // TODO
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
        }
      })
      this.cancel()
    },
    getList() {
      skuSellerOfflineSkuList({
        ...this.queryParams,
        offlineId: this.formData.id,
      }).then((res) => {
        this.tableData = res.rows
        this.total = res.total
      })
    },

    cancel() {
      this.$emit('update:show', false)
    },
    /** 重置按钮操作 */
    resetQuery() {
      Object.keys(this.queryParams).forEach((key) => (this.queryParams[key] = null))
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.getList()
    },
  },
}
</script>
<style scoped lang="scss"></style>
